Validate = {
	activate:function(jArea){
		try{jArea.undelegate('Validate');}catch(e){/*undelegate existing easyValidate Events */}
		jArea.delegate('input[validate]','keyup.EasyValidate',keyupEvent = function(){
			Validate.inputKeyup(jQuery(this));
		});
		jArea.delegate('input[validate]','focusout.EasyValidate',focusoutEvent = function(){
			Validate.inputFocusout(jQuery(this));
		});
		jArea.delegate('select[validate]','focusout.EasyValidate',focusoutEvent = function(){
			Validate.inputFocusout(jQuery(this));
		});
	},
	
	inputKeyup:function(jInput){
		var words = Validate.getWords(jInput);
		var inputString = jInput.val();
		if(words.match(/number/i)){
			inputString = Vw.number(inputString);
			if(words.match(/positive/i)){
				inputString = Vw.positive(inputString);
			}
			if(words.match(/maximum/i)) {
				inputString = Vw.maximum(inputString,words);
			}
			if(words.match(/integer/i) && !words.match(/money/i)){
				inputString = Vw.integer(inputString);
			}
		} else {
			if(words.match(/maxlength/i)){
				inputString = Vw.maxlength(inputString,words);
			}
		}
		if(jInput.val() != inputString){ // only overwrite page if inputString has been changed.
			Validate.setInputAndCursor(inputString,jInput.get(0));
		}
	},
	
	inputFocusout:function(jInput){
		var words = Validate.getWords(jInput);
		var inputString = jInput.val();
		if(words.match(/number/i)){
			inputString = Vw.numberFocusout(inputString,words);
			if(words.match(/money/i)){
				inputString = Vw.moneyFocusout(inputString);
			}
		}
		if(words.match(/email/i)){
			inputString = Vw.emailFocusout(jInput);
		}
		jInput.val(inputString);
	},
	
	setInputAndCursor: function(inputString,thisElement) { // relocates the cursor so typing flows naturally for keyup's
		var position = 0;
		if (document.selection) {
			thisElement.focus();
			var Sel = document.selection.createRange ();
			Sel.moveStart ('character', -thisElement.value.length);
			position = Sel.text.length;
		} else if (thisElement.selectionStart || thisElement.selectionStart == '0'){
			position = thisElement.selectionStart;
		}
		var offset = inputString.length - jQuery(thisElement).val().length;
		jQuery(thisElement).val(inputString);
		try{
			thisElement.setSelectionRange(position+offset,position+offset);
		}catch(e){
			// do nothing.  sometimes this fails because the element is no longer focused.
		}
	},
	
	getWords:function(jInput){
		var wordList = jInput.attr('validate') || "";
		// remove the float property that sneaks in to this list for whatever reason.
		wordList = wordList.replace("float","");
		wordList = wordList.toLowerCase();
		// if a number word, number filter is prerequisite
		if(wordList.indexOf('number') == -1 && (wordList.indexOf('integer') > -1 || wordList.indexOf('positive') > -1 || wordList.indexOf('money') > -1 || wordList.indexOf('maximum') > -1 || wordList.indexOf('minimum') > -1 || wordList.indexOf('money') > -1)){
			wordList = 'number,' + wordList;
		}
		return wordList;
	},
	
	getWordNumber: function(words,word) {
		var number = "";
		var location = words.toLowerCase().indexOf(word.toLowerCase());
		for(var i = location+word.length; i < words.length; i++){
			if(isNaN(words.charAt(i)) && words.charAt(i) != "."){
				break;
			} else {
				number += words.charAt(i);
			}
		}
		return number;
	},
	check: function(jArea){
		// check required
		var result = true;
		jArea.find('input[validate],select[validate]').each(function(){
			var jThis = jQuery(this);
			var value = jThis.val() || "";
			var words = Validate.getWords(jThis);
			if(words.toLowerCase().indexOf('required') > 0 && value == ""){
				jThis.addClass('validation_failed');
				var result = false;
			}
		});
		return false;
	}
};
jQuery(document).ready(function(){
	Validate.activate(jQuery(document));
});